/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Constant;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.CourseTradeDao;
import com.hboxs.asl.dao.PaymentMethodDao;
import com.hboxs.asl.entity.Course;
import com.hboxs.asl.entity.CourseTrade;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.PaymentMethod;
import com.hboxs.asl.pingpp.Constants;
import com.hboxs.asl.service.CourseService;
import com.hboxs.asl.service.CourseTradeService;
import com.hboxs.asl.service.FinancialRecordService;
import com.hboxs.asl.service.MemberService;
import com.pingplusplus.model.Charge;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Service - 课程流水
 */
@Service("courseTradeServiceImpl")
public class CourseTradeServiceImpl extends BaseServiceImpl<CourseTrade, Long> implements CourseTradeService {

    @Resource(name = "courseTradeDaoImpl")
    private CourseTradeDao courseTradeDao;
    @Resource(name = "paymentMethodDaoImpl")
    private PaymentMethodDao paymentMethodDao;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "financialRecordServiceImpl")
    private FinancialRecordService financialRecordService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    @Resource(name = "courseTradeDaoImpl")
    public void setBaseDao(CourseTradeDao courseTradeDao) {
        super.setBaseDao(courseTradeDao);
    }


    @Override
    public void dealCourseTrade(Charge charge, CourseTrade courseTrade) {
        PaymentMethod paymentMethod = findPaymentMethod(charge.getChannel());
        courseTrade.setPaymentMethod(paymentMethod);
        courseTrade.setPaymentMethodName(paymentMethod.getName());
        courseTrade.setPaymentStatus(CourseTrade.PaymentStatus.paid);
        Course course = courseService.find(courseTrade.getCourseId());
        //报名人数+1
        if (course != null) {
            course.setApplyCount(course.getApplyCount()+1);
            courseService.update(course);
            //课程支出记录
            financialRecordService.expenditureRecordCourse(courseTrade, null);
        }
        Member teacher = memberService.find(courseTrade.getTeacherId());
        if (teacher != null) {
            //老师余额增加
            teacher.setBalance(teacher.getBalance().add(courseTrade.getPrice()));
            //老师的收入
            teacher.setEarnings(teacher.getEarnings().add(courseTrade.getPrice()));
            memberService.update(teacher);
            //老师课程收入
            financialRecordService.incomeRecordCourse(courseTrade, teacher, null);
        }
        this.update(courseTrade);
    }

    @Override
    public void dealCourseTradeByPoint(CourseTrade courseTrade) {
        PaymentMethod paymentMethod = findPaymentMethod(Constants.POINT_AMOUNT_PAY);
        courseTrade.setPaymentMethod(paymentMethod);
        courseTrade.setPaymentMethodName(paymentMethod.getName());
        courseTrade.setPaymentStatus(CourseTrade.PaymentStatus.paid);
        Course course = courseService.find(courseTrade.getCourseId());
        //报名人数+1
        if (course != null) {
            course.setApplyCount(course.getApplyCount()+1);
            courseService.update(course);
            //课程支出记录
            financialRecordService.expenditureRecordCourse(courseTrade, Constant.POINT_PAY);
        }
        Member teacher = memberService.find(courseTrade.getTeacherId());
        if (teacher != null) {
            //老师余额增加
            teacher.setBalance(teacher.getBalance().add(courseTrade.getPrice()));
            //老师的收入
            teacher.setEarnings(teacher.getEarnings().add(courseTrade.getPrice()));
            memberService.update(teacher);
            //老师课程收入
            financialRecordService.incomeRecordCourse(courseTrade, teacher, Constant.POINT_PAY);
        }
        this.update(courseTrade);
    }

    @Override
    public CourseTrade findBySn(String sn) {
        return courseTradeDao.findBySn(sn);
    }

    @Override
    public Page<CourseTrade> allTrade(Pageable pageable, Member member, CourseTrade.PaymentStatus paymentStatus) {
        return courseTradeDao.allTrade(pageable, member, paymentStatus);
    }

    @Override
    public Page<CourseTrade> all(Pageable pageable, Member member, CourseTrade.CourseType courseType, CourseTrade.PaymentStatus paymentStatus) {
        return courseTradeDao.all(pageable, member, courseType, paymentStatus);
    }

    @Override
    public Page<CourseTrade> ing(Pageable pageable, Member member, CourseTrade.CourseType courseType, CourseTrade.PaymentStatus paymentStatus) {
        return courseTradeDao.ing(pageable, member, courseType, paymentStatus);
    }

    @Override
    public Page<CourseTrade> toEvaluate(Pageable pageable, Member member, CourseTrade.CourseType courseType, CourseTrade.PaymentStatus paymentStatus, Boolean hasReview) {
        return courseTradeDao.toEvaluate(pageable, member, courseType, paymentStatus, hasReview);
    }

    @Override
    public long findPaidCourse(Long courseId, Member buyer, CourseTrade.PaymentStatus paymentStatus) {
        return courseTradeDao.findPaidCourse(courseId, buyer, paymentStatus);
    }

    @Override
    public Page<CourseTrade> findPaidCourseTrades(Pageable pageable, Member buyer, CourseTrade.PaymentStatus paymentStatus, Long categoryId) {
        return courseTradeDao.findPaidCourseTrades(pageable, buyer, paymentStatus, categoryId);
    }

    @Override
    public long getCourseTradeCount(CourseTrade.PaymentStatus paymentStatus) {
        return courseTradeDao.getCourseTradeCount(paymentStatus);
    }

    @Override
    public CourseTrade courseTradeCount(CourseTrade.PaymentStatus paymentStatus, Member member, Long courseId) {
        return courseTradeDao.courseTradeCount(paymentStatus, member, courseId);
    }

    @Override
    public void updateBatch(Long courseId, CourseTrade.CourseType courseType) {
        courseTradeDao.updateBatch(courseId, courseType);
    }

    /**
     * 查找对应的支付方式
     */
    private PaymentMethod findPaymentMethod(String channel) {
        List<PaymentMethod> paymentMethods = paymentMethodDao.findList(null, null, null, null);
        HashMap<String, Integer> modeOfPayment = new HashMap<>();
        modeOfPayment.put(Constants.CHANNEL_WX, 0);
        modeOfPayment.put(Constants.CHANNEL_ALIPAY, 1);
        modeOfPayment.put(Constants.CHANNEL_UPACP, 2);
        modeOfPayment.put(Constants.CHANNEL_AMOUNT_PAY, 3);
        modeOfPayment.put(Constants.POINT_AMOUNT_PAY, 5);
        return paymentMethods.get(modeOfPayment.get(channel));
    }
}
